#### Code for visualizations ####
#### "The Pandemic was a Global Exam and Our Country Came In First" ###
#### Perspectives on Politics ###
#### Date of final submission 16 September 2023 ###
#### Author: Bruno Schmidt-Feuerheerd ### 
#### email: bs674@cam.ac.uk ### 

# data source: https://github.com/OxCGRT/covid-policy-tracker/blob/master/data/timeseries/confirmed_cases.csv 

# setwd("~/Documents/Uni/PhD/Projects/GRM2022/data")

library(rlang)
library(ggplot2)
library(ggpattern)
library(data.table)
library(lubridate)
library(readr)

# cleaned in Excel to only include data on China, Saudi Arabia and the United States
file <- read.csv("owid-covid-data.csv", sep = ";", header = TRUE)

# Convert the character variable to a date format
file$date <- as.Date(file$date, format = "%d.%m.%y")

#### Visualization cumulative cases per million population ####
ggplot(file, aes(x= date, y = total_cases_per_million, color = location)) +
  geom_line() + xlab("Year") + ylab("COVID-19 Cases/1,000,000 pop.")+
  scale_y_continuous("COVID-19 Cases/1,000,000 pop.",
                     limits = c(0, 320000), breaks = seq(0, 320000, by = 50000))+
  scale_color_manual("",values = c("firebrick", "forestgreen", "dodgerblue"))+
  annotate(geom = "label", label = "China",
           x = as.Date("2023-4-6"),
           y = 85000, color = "firebrick")+
  annotate(geom = "label", label = "Saudi Arabia",
           x = as.Date("2023-4-6"),
           y = 40000, color = "forestgreen")+
  annotate(geom = "label", label = "USA",
           x = as.Date("2023-4-6"),
           y = 280000, color = "dodgerblue")+
  theme_bw()+
  theme(text = element_text(colour = "black", size = 12),
        legend.position="none")

#### Visualization cumulative deaths per million population ####
ggplot(file, aes(x= date, y = total_deaths_per_million, color = location)) +
  geom_line() + xlab("Year") + ylab("COVID-19 Deaths/1,000,000 pop.")+
  scale_y_continuous("COVID-19 Deaths/1,000,000 pop.",
                     limits = c(0, 3500), breaks = seq(0, 3500, by = 500))+
  scale_color_manual("",values = c("firebrick", "forestgreen", "dodgerblue"))+
  annotate(geom = "label", label = "China",
           x = as.Date("2022-8-8"),
           y = 140, color = "firebrick")+
  annotate(geom = "label", label = "Saudi Arabia",
           x = as.Date("2023-4-6"),
           y = 500, color = "forestgreen")+
  annotate(geom = "label", label = "USA",
           x = as.Date("2023-4-6"),
           y = 3000, color = "dodgerblue")+
  theme_bw()+
  theme(text = element_text(colour = "black", size = 12),
        legend.position="none")

#### Visualization stringency index from 0-100 ####
ggplot(file, aes(x= date, y = stringency_index, color = location)) +
  geom_line() + xlab("Year") + ylab("Stringency Index.")+
  scale_y_continuous("Stringency Index",
                     limits = c(0, 100), breaks = seq(0, 100, by = 20))+
  scale_color_manual("",values = c("firebrick", "forestgreen", "dodgerblue"))+
  annotate(geom = "label", label = "China",
           x = as.Date("2023-4-6"),
           y = 80, color = "firebrick")+
  annotate(geom = "label", label = "Saudi Arabia",
           x = as.Date("2023-4-6"),
           y = 10, color = "forestgreen")+
  annotate(geom = "label", label = "USA",
           x = as.Date("2023-4-6"),
           y = 30, color = "dodgerblue")+
  theme_bw()+
  theme(text = element_text(colour = "black", size = 12),
        legend.position="none")


#### Bar charts public opinion based on Arab Opinion Index ####
#### Threat perception ####

# Sample data with custom order for categories
data <- data.frame(
  Category = c("No threat","Arab Countries", "Iran", "Israel", "The US"),
  Value = c(38.3, 2.0, 15.5, 2.4, 2.0)
)

# Define custom colors for each category
custom_colors <- c("blue", "dark green", "red", "dark blue", "yellow") 


# Create a bar plot using ggplot2 with custom line types and colors
ggplot(data, aes(x = reorder(Category, -Value), y = Value, fill = Category, linetype = Category)) +
  geom_bar(stat = "identity") +
  scale_fill_manual(values = custom_colors) +
  labs(title = "Public Threat Perception in Saudi Arabia, 2022, in per cent", x = "", y = "") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, size = 10), legend.position = "none")


#### Readiness for Democracy ####
data_d <- data.frame(
  Category_d = c("Strongly Disagree","Disagree", "Agree", "Strongly Agree"),
  Value_d = c(8.9, 18.8, 19.3, 5.7)
)

# Specify the order of the categories as you want them in the plot
category_order_d <- c("Strongly Disagree","Disagree", "Agree", "Strongly Agree")
data_d$Category_d <- factor(data_d$Category_d, levels = category_order_d)

# Define custom colors for each category
custom_colors_d <- c("dark red", "red", "green", "dark green")

# Create a bar plot using ggplot2 with custom line types and colors
ggplot(data_d, aes(x = Category_d, y = Value_d, fill = Category_d, linetype = Category_d)) +
  geom_bar(stat = "identity") +
  scale_fill_manual(values = custom_colors_d) +
  scale_linetype_manual(values = custom_linetypes_d) +
  labs(title = "Our society is unprepared for democracy, 2022, in per cent", x = "", y = "") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, size = 12), legend.position = "none")


